//
//  SEGPropertyTableViewCell.m
//  ExcellentHome
//
//  Created by mac on 2017/11/14.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGPropertyTableViewCell.h"
#import "SEGSubMessageModel.h"

// 图片宽高比
static CGFloat const kSEGProperty_ImageRate = 1.78;

@interface SEGPropertyTableViewCell ()

@property (nonatomic, strong) UIView *bgView;    //白色背景图
@property (nonatomic, strong)UILabel *titleLabel;   //标题文本
@property (nonatomic, strong)UILabel *detailLabel;  //内容详情文本
@property (nonatomic, strong)UIImageView *propertyImage;    //物业图片
@property (nonatomic, strong)UIView *line;  //查看详情上的分割线
@property (nonatomic, strong)UILabel *viewDetailLabel;    //查看详情
@property (nonatomic, strong)UIImageView *arrowImageView;    //查看详情箭头

@property (nonatomic, strong) UILabel *shopStatusLabel;
@property (nonatomic, strong) UILabel *shopTimeLabel;
@end

@implementation SEGPropertyTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    self.backgroundColor = kColor_bg;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
//        self.frame = CGRectMake(0, 0, LYScreenWidth(), 191);
    [self setupUI];
//    }
    return self;
}

-(void)setupUI{
    
    [self.contentView addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
        make.top.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView);
    }];
    
    [self.bgView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top).mas_offset(18);
        make.left.equalTo(self.bgView.mas_left).mas_offset(15);
        make.right.equalTo(self.bgView.mas_right).mas_offset(-15);
    }];
    
    [self.bgView addSubview:self.propertyImage];
    [self.bgView addSubview:self.detailLabel];
    [self.bgView addSubview:self.shopStatusLabel];
    [self.bgView addSubview:self.shopTimeLabel];
    
}

- (void)cellWithModel:(MessageModel *)msgModel
{
    [self resetCell];
    BOOL canJump = [self seg_canJumpToDetailWithModel:msgModel];
    if ([msgModel.groupid hasPrefix:kMessageGroupMall]) {
        [self seg_showViewWithMallModel:msgModel];
    }else {
        if (msgModel.submsg && msgModel.submsg.length > 0) {
            //解析JSON
            NSDictionary* subMsgDic = [NSJSONSerialization JSONObjectWithData:[msgModel.submsg dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];
            if (subMsgDic) {
                SEGSubMessageModel *subMsgModel = [SEGSubMessageModel initWithDictionary:subMsgDic];
                self.titleLabel.text = subMsgModel.title;
                if (subMsgModel.picture && subMsgModel.picture.length > 0) {//图文
                    [self seg_showViewWithImageString:subMsgModel.picture canJump:canJump];
                }else {//纯文本
                    [self seg_showViewWithTitle:subMsgModel.title message:subMsgModel.content canJump:canJump];
                }
            }
        }
        else {//老数据结构：纯文本
            [self seg_showViewWithTitle:msgModel.title message:msgModel.message canJump:canJump];
        }
    }
}
//重设cell
- (void)resetCell {
    [self.propertyImage removeFromSuperview];
    [self.detailLabel removeFromSuperview];
    [self.shopStatusLabel removeFromSuperview];
    self.line.hidden = YES;
    self.viewDetailLabel.hidden = YES;
    self.arrowImageView.hidden = YES;
    
    [self.shopTimeLabel removeFromSuperview];
    self.detailLabel.numberOfLines = 0;
}

- (BOOL)seg_canJumpToDetailWithModel:(MessageModel *)msgModel
{
    NSString *type = msgModel.type;
    //帮帮
    if ([msgModel.groupid hasPrefix:kMessageGroupRadioPre]) { //帮帮广播 ---
        return YES;
    }
    
    //活动通知
    if ([msgModel.groupid hasPrefix:kMessageGroupActivity]) {
        if ([type isEqualToString:kMessageTypeOffLineActivityReview]) {// 线下活动审核信息
            if([msgModel.turnTip isEqualToString:@"2"]) {//审核通过 跳邀请函
                return YES;
            }
            if ([msgModel.turnTip isEqualToString:@"3"]) {
                if ([msgModel.turnType isEqualToString:@"1"] || [msgModel.turnType isEqualToString:@"2"]) {//审核不通过，跳活动详情
                    return YES;
                }else {
                    return NO;
                }
            }
            return NO;
        }
        if ([type isEqualToString:kMessageTypeCUSTOM]) {//自定义
            NSLog(@"msgModel.turnType");
            if (msgModel.turnType && msgModel.turnType.length > 0) {
                return YES;
            }else {
                return NO;
            }
        }
        
        if ([type isEqualToString:kMessageTypeWinningPublicActivity] ||
            [type isEqualToString:kMessageTypeWinningPublicTopic] ||
            [type isEqualToString:kMessageTypeWinningPublicIntegral] ||
            [type isEqualToString:kMessageTypeTopic] ||
            [type isEqualToString:kMessageTypeIntegral] ||
            [type isEqualToString:kMessageTypeINVITENEIGHBOR] ||
            [type isEqualToString:kMessageTypeActivityDetail])
        {
            return YES;
        }
    }
    
    //系统消息
    if ([type isEqualToString:kMessageTypeUserFeedbackReply] ||
        [type isEqualToString:SEGIntegralCode.signup] ||
        [type isEqualToString:SEGIntegralCode_LEVEL_DOWNGRADE] ||
        [type isEqualToString:SEGIntegralCode_PUSH_TYPE_TELEPHONE_FARE] ||
        [type isEqualToString:kMessageTypeShareGift] ||
        [type isEqualToString:kMessageTypeInvitationSuccess] ||
        [type isEqualToString:kMessageTypeYOUZANOrder]) {
        return YES;
    }
    
    //物业服务
    if ([type isEqualToString:kMessageTypeNews] ||
        [type isEqualToString:kMessageTypeExpressCollection] ||
        [type isEqualToString:kMessageTypeService] ||
        [type isEqualToString:kMessageTypeBill] ||
        [type isEqualToString:kMessageTypeQuestionnaire] ||
        [type isEqualToString:kMessageTypeUserCheck]||
        [type isEqualToString:kMessageTypeHouseRentalAndSale]||
        [type isEqualToString:kMessageTypeCarRentalAndSale]) {
        return YES;
    }
    
    if ([msgModel.groupid hasPrefix:kMessageGroupRecommendPre]) { //精选推荐
        return YES;
    }
    
    //车场消息
    if ([type isEqualToString:kMessageTypeCheckMonthCard] ||
        [type isEqualToString:kMessageTypeMonthCardActiveRemind] ||
        [type isEqualToString:kMessageTypeMonthCardExpireRemind] ||
        [type isEqualToString:kMessageTypeAddCar]) {
        return YES;
    }
    
    return NO;
}

/// 图文
- (void)seg_showViewWithImageString:(NSString *)imageString canJump:(BOOL)canJump {
    [self.bgView addSubview:self.propertyImage];

    CGFloat width = LYScreenWidth() - 60;
    CGFloat height = width/kSEGProperty_ImageRate;
    if(canJump) {
        [self.propertyImage mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom).mas_offset(17);
            make.height.mas_equalTo(height);
            make.centerX.equalTo(self.bgView.mas_centerX);
            make.width.mas_equalTo(width);
            make.bottom.equalTo(self.bgView).offset(-50);
        }];
        self.viewDetailLabel.hidden = NO;
        self.arrowImageView.hidden = NO;
    } else {
        [self.propertyImage mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom).mas_offset(17);
            make.height.mas_equalTo(height);
            make.centerX.equalTo(self.bgView.mas_centerX);
            make.width.mas_equalTo(width);
            make.bottom.equalTo(self.bgView).offset(-15);
        }];
        self.viewDetailLabel.hidden = YES;
        self.arrowImageView.hidden = YES;
    }
    [self.propertyImage setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", kClientImageRootURL, imageString]]
                       placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_690_240")] options:SDWebImageRetryFailed];
}

/// 纯文本
- (void)seg_showViewWithTitle:(NSString *)title message:(NSString *)message canJump:(BOOL)canJump {
    [self.bgView addSubview:self.detailLabel];
    NSInteger detailLabel_marginTop = 0;
    if (message.length > 0) detailLabel_marginTop = 15;
    if (canJump) {
        [self.detailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom).mas_offset(detailLabel_marginTop);
            make.left.equalTo(self.bgView.mas_left).mas_offset(15);
            make.right.equalTo(self.bgView.mas_right).mas_offset(-15);
            make.bottom.equalTo(self.bgView).offset(-15-50);
        }];
        self.line.hidden = NO;
        self.viewDetailLabel.hidden = NO;
        self.arrowImageView.hidden = NO;
    }else {
        [self.detailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom).mas_offset(detailLabel_marginTop);
            make.left.equalTo(self.bgView.mas_left).mas_offset(15);
            make.right.equalTo(self.bgView.mas_right).mas_offset(-15);
            make.bottom.equalTo(self.bgView).offset(-15);
        }];
        self.line.hidden = YES;
        self.viewDetailLabel.hidden = YES;
        self.arrowImageView.hidden = YES;
    }

    
    // 按服务器返回的样式
    if ([message hasSuffix:@"</font>"]) {
        NSMutableAttributedString* attributedString = [[NSMutableAttributedString alloc] initWithData:[message dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        
        [attributedString addAttribute:NSFontAttributeName
                                 value:[UIFont systemFontOfSize:13]
                                 range:NSMakeRange(0, [attributedString length])];
        
        self.detailLabel.attributedText = attributedString;
    }else {
        self.detailLabel.text = message;
    }
    self.detailLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    self.titleLabel.text = title;
}

//商场消息
- (void)seg_showViewWithMallModel:(MessageModel *)msgModel {
    self.detailLabel.numberOfLines = 2;
    [self.bgView addSubview:self.propertyImage];
    [self.bgView addSubview:self.detailLabel];
    [self.bgView addSubview:self.shopStatusLabel];
    [self.bgView addSubview:self.shopTimeLabel];
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView).mas_offset(20);
        make.left.equalTo(self.bgView).mas_offset(15);
    }];
    
    self.shopTimeLabel.textAlignment = NSTextAlignmentRight;
    [self.shopTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView).mas_offset(23);
        make.left.equalTo(self.titleLabel.mas_right).mas_offset(5);
        make.right.equalTo(self.bgView).mas_offset(-15);
    }];
    [self.shopTimeLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
    [self.titleLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];

    [self.propertyImage mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).mas_offset(15);
        make.width.height.mas_equalTo(60);
        make.left.equalTo(self.titleLabel);
        make.bottom.equalTo(self.bgView).mas_offset(-20);
    }];
    
    [self.detailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.propertyImage).mas_offset(1.5);
        make.left.equalTo(self.propertyImage.mas_right).mas_offset(15);
        make.right.equalTo(self.bgView.mas_right).mas_offset(-15);
    }];
    
    [self.shopStatusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.detailLabel.mas_bottom).mas_offset(4);
        make.left.equalTo(self.detailLabel);
        make.right.equalTo(self.bgView.mas_right).mas_offset(-15);
    }];
    
    
    NSDictionary* subMsgDic = [NSJSONSerialization JSONObjectWithData:[msgModel.submsg dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];
    SEGSubMessageModel *subMsgModel = [SEGSubMessageModel initWithDictionary:subMsgDic];

    self.titleLabel.text = msgModel.title;
    self.detailLabel.attributedText = [SEGUtils getAttributeStringWith:kColor_1 font:[UIFont systemFontOfSize:12] textStr:msgModel.message lineSpace:5 alignment:NSTextAlignmentLeft];
    self.detailLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    self.shopStatusLabel.text = subMsgModel.orderStatus;

    if (ValueToString(subMsgModel.orderTime).length > 0) {
        self.shopTimeLabel.text = [NSDate getDateStringWithTimestamp:ValueToString(subMsgModel.orderTime) dateFormate:@"YYYY/MM/dd" timestmapType:SEGEnumTimerstamp_Second];
    } else if (ValueToString(msgModel.createTime).length > 0) {
        self.shopTimeLabel.text = ValueToString(msgModel.createTime);
    } else {
        self.shopTimeLabel.text = @"";
    }
    [self.propertyImage setImageWithURL:[NSURL URLWithString:subMsgModel.pic]
    placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_70_70")] options:SDWebImageRetryFailed];
}

#pragma mark - UITraitEnvironment
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    [super traitCollectionDidChange:previousTraitCollection];
    self.bgView.layer.borderColor = kColor_L4.CGColor;
}

#pragma mark ----------------------- getter
-(UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = kColor_W;
        _bgView.layer.cornerRadius = 4;
        _bgView.layer.masksToBounds = YES;
        _bgView.layer.borderColor = kColor_L4.CGColor;
        _bgView.layer.borderWidth = 0.5;
    }
    return _bgView;
}

-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = kColor_1;
        _titleLabel.font = kFont_16;
        _titleLabel.numberOfLines = 2;
    }
    return _titleLabel;
}

- (UILabel *)shopStatusLabel {
    if (!_shopStatusLabel) {
        _shopStatusLabel = [[UILabel alloc] init];
        _shopStatusLabel.textColor = kColor_3;
        _shopStatusLabel.font = [UIFont systemFontOfSize:12];
        _shopStatusLabel.numberOfLines = 1;
    }
    return _shopStatusLabel;
}

- (UILabel *)shopTimeLabel {
    if (!_shopTimeLabel) {
        _shopTimeLabel = [[UILabel alloc] init];
        _shopTimeLabel.textColor = kColor_3;
        _shopTimeLabel.font = [UIFont systemFontOfSize:12];
        _shopTimeLabel.numberOfLines = 1;
    }
    return _shopTimeLabel;
}

-(UILabel *)detailLabel{
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc] init];
        _detailLabel.textColor = kColor_3;
        _detailLabel.font = [UIFont systemFontOfSize:13];
        _detailLabel.numberOfLines = 0;
    }
    return _detailLabel;
}

-(UIImageView *)propertyImage{
    if (!_propertyImage) {
        _propertyImage = [[UIImageView alloc] init];
        _propertyImage.contentMode = UIViewContentModeScaleAspectFill;
        _propertyImage.layer.masksToBounds = YES;
    }
    return _propertyImage;
}

- (UIView *)line
{
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = kColor_L2;
        [self.bgView addSubview:_line];
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.bgView);
            make.bottom.equalTo(self.bgView).offset(-50);
            make.height.mas_offset(0.5);
        }];
    }
    return _line;
}

- (UILabel *)viewDetailLabel
{
    if (!_viewDetailLabel) {
        _viewDetailLabel = [[UILabel alloc] init];
        _viewDetailLabel.textColor = kColor_1;
        _viewDetailLabel.font = kFont_15;
        _viewDetailLabel.text = @"查看详情";
        [self.bgView addSubview:_viewDetailLabel];
        [_viewDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bgView).offset(15);
            make.bottom.equalTo(self.bgView);
            make.height.mas_equalTo(50);
        }];
    }
    return _viewDetailLabel;
}

-(UIImageView *)arrowImageView
{
    if (!_arrowImageView) {
        _arrowImageView = [[UIImageView alloc] init];
        _arrowImageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"btn_more_gray")];
        [self.bgView addSubview:_arrowImageView];
        [_arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.bgView).offset(-15);
            make.centerY.equalTo(self.viewDetailLabel);
        }];
    }
    return _arrowImageView;
}

@end
